<template>
  <div>
    <a-drawer
      :title="title"
      placement="right"
      :closable="true"
      :width="width"
      :visible="visible"
      @close="handleCancel"
      destroyOnClose
    >
      <a-spin :spinning="confirmLoading">
        <j-form-container :disabled="formDisabled">
          <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
            <a-row>
              <a-col :span="24">
                <a-form-model-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerId">
                  <a-input-group compact>
                    <a-input v-model="customerName" style="width: 75%" disabled></a-input>
                    <a-button type="primary" style="width: 25%" @click="chooseCustomer">选择</a-button>
                    <!--<a-select
                      show-search
                      :allowClear="true"
                      placeholder="请选择客户"
                      option-filter-prop="children"
                      v-model="customerName"
                      style="width: 85%"
                      disabled>
                      <a-select-option :value="item.id" v-for="(item,index) in customerData" :key="index">
                        {{item.name}}
                      </a-select-option>
                    </a-select>-->
                  </a-input-group>
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row>
              <a-col :span="24">
                <a-form-model-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectName">
                  <a-input v-model="model.projectName" placeholder="请输入项目名称" style="width: 100%"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="劳务类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="labourType">
                  <j-dict-select-tag style="width: 100%"
                                     placeholder="请选择劳务类型"
                                     dictCode="customer_labour_distribute_labour_type"
                                     v-model="model.labourType"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="劳务金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="labourMoney">
                  <a-input-number v-model="model.labourMoney" placeholder="请输入劳务金额" style="width: 100%"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="项目地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectAddress">
                  <a-input v-model="model.projectAddress" placeholder="请输入项目地址"></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="劳务开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="labourBeginTime">
                  <j-date placeholder="请选择劳务开始时间" v-model="model.labourBeginTime" :show-time="true"
                          date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="劳务结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="labourEndTime">
                  <j-date placeholder="请选择劳务结束时间" v-model="model.labourEndTime" :show-time="true"
                          date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="工期/天" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workDeadline">
                  <a-input-number v-model="model.workDeadline" placeholder="请输入工期" style="width: 100%"/>
                </a-form-model-item>
              </a-col>

              <a-col :span="24">
                <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remarks">
                  <a-input v-model="model.remarks" placeholder="请输入备注"></a-input>
                </a-form-model-item>
              </a-col>

              <a-col :span="24">
                <a-form-model-item label="工作图纸" prop="workBlueprintList" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <j-upload v-model="workBlueprintList" text="上传" fileType="image"></j-upload>
                </a-form-model-item>
              </a-col>

              <a-col :span="24">
                <a-form-model-item label="工程量清单" prop="quantityInventoryList" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <j-upload v-model="quantityInventoryList" text="上传" fileType="image"></j-upload>
                </a-form-model-item>
              </a-col>

              <a-col :span="24">
                <a-form-model-item label="附件" prop="annexList" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <j-upload v-model="annexList"></j-upload>
                </a-form-model-item>
              </a-col>

            </a-row>
          </a-form-model>
        </j-form-container>
      </a-spin>
      <a-button type="primary" style="width: 100%" @click="handleOk">保存</a-button>
    </a-drawer>

    <choose-customer-modal ref="chooseCustomerModal" @selectFinished="chooseCustomerOk"></choose-customer-modal>
  </div>

</template>

<script>
  import {httpAction, getAction} from '@/api/manage'
  import chooseCustomerModal from "./chooseCustomerModal";

  export default {
    name: "addLabourModal",
    components: {
      chooseCustomerModal,
    },
    data() {
      return {
        model: {},
        title: '',
        customerName:"",
        width: "100%",
        visible: false,
        workBlueprintList: [],
        quantityInventoryList: [],
        annexList:[],
        customerData:[],
        currentCustomerId: 0,
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        validatorRules: {
          labourType: [{ required: true, message: '劳务类型不能为空' }],
          labourMoney: [
            { required: true, message: '劳务金额不能为空' },
            {"pattern": /^\d+(?=\.{0,1}\d+$|$)/,"message": "只能输入正数"}
          ],
          projectAddress: [{ required: true, message: '项目地址不能为空' }],
          labourBeginTime: [{ required: true, message: '劳务起始时间不能为空' }],
          labourEndTime: [{ required: true, message: '劳务结束时间不能为空' }],
          workDeadline:[
            { required: true, message: '工期不能为空' },
            {"pattern": /^[0-9]*$/,"message": "只能输入正整数"},
          ],
        },
        url: {
          add: "/ghCustomerLabourDistribute/add",
          customerDataList: "/ghCustomerInfo/queryAllCustomer",
          edit: "/ghCustomerLabourDistribute/edit",
        }
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      },
    },
    created() {

    },
    methods: {
      add() {
        this.workBlueprintList = [];
        this.quantityInventoryList = [];
        this.annexList = [];
        this.model = {};
        this.customerName = "";
        this.visible = true;
        this.loadCustomerData()
      },
      edit(record){
        this.customerName = record.customerName;
        this.currentCustomerId = record.customerId;
        this.model = Object.assign({}, record)
        if (record.workBlueprintList != null && record.workBlueprintList.length != 0){
          this.workBlueprintList = [...record.workBlueprintList]
        }
        if (record.quantityInventoryList != null && record.quantityInventoryList.length != 0){
          this.quantityInventoryList = [...record.quantityInventoryList]
        }
        if (record.annexList != null && record.annexList.length != 0){
          this.annexList = [...record.annexList]
        }
        this.visible = true;
        this.loadCustomerData();
      },
      handleCancel() {
        this.visible = false;
        this.loadCustomerData()
      },
      loadCustomerData(){
        getAction(this.url.customerDataList).then((res) => {
          if (res.success) {
            this.customerData = res.result.records||res.result;
          }
        })
      },
      changeCustomer(value){
        this.currentCustomerId = value;
      },
      //选择客户
      chooseCustomer(){
        this.$refs.chooseCustomerModal.add();
        this.$refs.chooseCustomerModal.title = "选择客户";
      },
      chooseCustomerOk(obj){
        this.customerName = obj.name
        this.currentCustomerId = obj.id;
      },
      close () {
        this.visible = false;
        this.customerName = "";
        this.workBlueprintList = [];
        this.quantityInventoryList = [];
        this.annexList = [];
        this.model = {};
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            var oDate1 = new Date(this.model.labourBeginTime);
            var oDate2 = new Date(this.model.labourEndTime);
            if(oDate1.getTime() >= oDate2.getTime()){
              this.$message.error("劳务开始时间不能大于劳务结束时间")
              return
            }
            if (this.currentCustomerId == null || this.currentCustomerId == 0){
              this.$message.error("请选择客户")
              return;
            }
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            }else {
              httpurl += this.url.edit;
              method = 'put';
            }
            if (this.workBlueprintList.length > 0){
              this.model.workBlueprint = this.workBlueprintList.toString();
            }else {
              this.model.workBlueprint = ""
            }
            if (this.quantityInventoryList.length > 0){
              this.model.quantityInventory = this.quantityInventoryList.toString();
            }else {
              this.model.quantityInventory = ""
            }
            if (this.annexList.length > 0){
              this.model.annex = this.annexList.toString();
            }else {
              this.model.annex = ""
            }
            this.model.customerId = this.currentCustomerId;
            httpAction(httpurl, this.model, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.model = {};
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
    }
  }
</script>

<style scoped>

</style>